package com.ruoyi.jinchuguanli.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cangku.mapper.KucunguanliMapper;
import com.ruoyi.cangku.mapper.ShangpinguanliMapper;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gongjulei;
import com.ruoyi.jinchuguanli.domain.*;
import com.ruoyi.jinchuguanli.mapper.GouhuorukuMapper;
import com.ruoyi.jinchuguanli.service.IGouhuorukuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.gongjulei.aReturnFormat;

/**
 * rukuService业务层处理
 *
 * @author ruoyi
 * @date 2023-11-08
 */
@Service
public class GouhuorukuServiceImpl implements IGouhuorukuService {
    @Autowired
    private GouhuorukuMapper gouhuorukuMapper;

    @Autowired
    private KucunguanliMapper kucunguanliMapper;

    @Autowired
    private ShangpinguanliMapper shangpinguanliMapper;

    /**
     * 查询ruku
     *
     * @param id ruku主键
     * @return ruku
     */
    @Override
    public Gouhuoruku selectGouhuorukuById (Integer id) {
        return gouhuorukuMapper.selectGouhuorukuById(id);
    }

    /**
     * 查询ruku列表
     *
     * @param gouhuoruku ruku
     * @return ruku
     */
    @Override
    public List<Gouhuoruku> selectGouhuorukuList (Gouhuoruku gouhuoruku) {
        return gouhuorukuMapper.selectGouhuorukuList(gouhuoruku);
    }

    /**
     * 新增ruku
     *
     * @param gouhuoruku ruku
     * @return 结果
     */
    @Override
    public int insertGouhuoruku (Gouhuoruku gouhuoruku) {

        gongjulei.kucunUpdate(gouhuoruku.getShangpinbianhao(), kucunguanliMapper, "+");
        List<ShangPinCun> shangPinCunList = JSONArray.parseArray(gouhuoruku.getShangpinbianhao(),ShangPinCun.class);
        gouhuoruku.setShangpinbianhao(JSONObject.toJSONString(shangPinCunList));
        return gouhuorukuMapper.insertGouhuoruku(gouhuoruku);
    }

    /**
     * 修改ruku
     *
     * @param gouhuoruku ruku
     * @return 结果
     */
    @Override
    public int updateGouhuoruku (Gouhuoruku gouhuoruku) {
        Gouhuoruku gouhuoruku1 = gouhuorukuMapper.selectGouhuorukuById(gouhuoruku.getId());
        gongjulei.kucunUpdate(gouhuoruku.getShangpinbianhao(), gouhuoruku1.getShangpinbianhao(), kucunguanliMapper, "+");
        List<ShangPinCun> shangPinCunList = JSONArray.parseArray(gouhuoruku.getShangpinbianhao(),ShangPinCun.class);
        gouhuoruku.setShangpinbianhao(JSONObject.toJSONString(shangPinCunList));
        return gouhuorukuMapper.updateGouhuoruku(gouhuoruku);
    }

    /**
     * 批量删除ruku
     *
     * @param ids 需要删除的ruku主键
     * @return 结果
     */
    @Override
    public int deleteGouhuorukuByIds (String ids) {
        String[] strings = Convert.toStrArray(ids);
        for (String id:strings
        ) {
            Gouhuoruku gouhuoruku = gouhuorukuMapper.selectGouhuorukuById(Integer.valueOf(id));
            GouhuorukuReturn gouhuorukuReturn = gongjulei.aReturnFormat(gouhuoruku,shangpinguanliMapper);
            gongjulei.kucunUpdate(JSONObject.toJSONString(gouhuorukuReturn.getShangpin()),kucunguanliMapper,"-");
        }
        return gouhuorukuMapper.deleteGouhuorukuByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除ruku信息
     *
     * @param id ruku主键
     * @return 结果
     */
    @Override
    public int deleteGouhuorukuById (Integer id) {
        Gouhuoruku gouhuoruku = gouhuorukuMapper.selectGouhuorukuById(id);
        GouhuorukuReturn gouhuorukuReturn = gongjulei.aReturnFormat(gouhuoruku,shangpinguanliMapper);
        gongjulei.kucunUpdate(JSONObject.toJSONString(gouhuorukuReturn.getShangpin()),kucunguanliMapper,"-");
        return gouhuorukuMapper.deleteGouhuorukuById(id);
    }

    @Override
    public List<GouhuorukuReturn> selectGouhuorukuReturnList (Gouhuoruku gouhuoruku) {
        List<Gouhuoruku> gouhuorukus = gouhuorukuMapper.selectGouhuorukuList(gouhuoruku);
        List<GouhuorukuReturn> gouhuorukuReturns = new ArrayList<>();
        for (Gouhuoruku g : gouhuorukus) {
//            GouhuorukuReturn gouhuorukuReturn = new GouhuorukuReturn();
//            BeanUtils.copyProperties(g, gouhuorukuReturn);
//
//            List<Shangpin> shangpinList = JSONObject.parseArray(g.getShangpinbianhao(), Shangpin.class);
//
//            gouhuorukuReturn.setShangpin(shangpinList);
            GouhuorukuReturn gouhuorukuReturn = gongjulei.aReturnFormat(g,shangpinguanliMapper);
            gouhuorukuReturns.add(gouhuorukuReturn);
        }
        return gouhuorukuReturns;
    }

    @Override
    public int updateGouhuorukuReturn (GouhuorukuReturn gr) {
        Gouhuoruku gouhuoruku = new Gouhuoruku();
        BeanUtils.copyProperties(gr, gouhuoruku);
//        String jsonStr = JSONObject.toJSONString(gr.getShangpin());
//        gouhuoruku.setShangpinbianhao(jsonStr);
        List<ShangPinCun> shangPinCunList = JSONArray.parseArray(JSONObject.toJSONString(gr.getShangpin()),ShangPinCun.class);
        gouhuoruku.setShangpinbianhao(JSONObject.toJSONString(shangPinCunList));
        return gouhuorukuMapper.updateGouhuoruku(gouhuoruku);
    }

    @Override
    public GouhuorukuReturn aReturnFormat(Gouhuoruku xc) {
        return gongjulei.aReturnFormat(xc,shangpinguanliMapper);
    }
}
